<template>
  <div class="dc-container">
    <el-dialog v-model="visible" width="50%"
      :fullscreen="false" :title="title" :close-on-click-modal="false"
      @open="onDialogOpen" @close="onDialogClose">
      <el-form ref="editForm" :model="form" :rules="rules" label-width="100px" :disabled="mode == 'view'">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item prop="company.id" label="公司">
              <el-cascader v-model="form.company.id" :options="companyOptions"
                separator="/" placeholder="请选择公司" clearable filterable
                :props="{ label: 'name', value: 'id', children: 'children', checkStrictly: true, emitPath: false }"
                :style="{ width: '100%' }"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="code" label="编号">
              <el-input v-model="form.code" maxLength="64" placeholder="请输入编号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="name" label="名称">
              <el-input v-model="form.name" maxLength="128" placeholder="请输入名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="isLocked" label="禁用">
              <el-select v-model="form.isLocked" placeholder="请选择是否禁用" clearable style="width: 100%">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="remarks" label="备注">
              <el-input v-model="form.remarks" type="textarea" maxLength="255" placeholder="请输入备注" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onSubmit" type="primary" v-show="mode != 'view'">保存</el-button>
          <el-button @click="onDialogClose" v-if="mode != 'view'">取消</el-button>
          <el-button @click="onDialogClose" v-if="mode == 'view'">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleById, saveRole } from '@/api/sys/role'
import { treeCompany } from '@/api/org/company'
import BaseUI from '@/views/components/baseUI'
import OperationIcon from '@/components/OperationIcon'

export default {
  extends: BaseUI,
  name: 'role-form',
  components: {
    OperationIcon
  },
  data() {
    return {
      mode: 'add',
      form: this.initForm(),
      visible: false,
      title: '角色',
      rules: {
        'company.id': [{ required: true, message: '公司不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        isLocked: [{ required: true, message: '禁用不能为空', trigger: 'blur' }]
      },
      companyOptions: []
    }
  },
  props: {
    permission: {
      type: Object
    }
  },
  methods: {
    async onSubmit() {
      let validFlag = true
      await this.$refs.editForm.validate((valid) => {
        if (!valid) {
          validFlag = false
          return false
        }
      })
      if (validFlag) {
        this.doSave()
      }
    },
    doSave() {
      this.setLoad()
      saveRole(this.form)
        .then((responseData) => {
          if (responseData.code == 100) {
            this.visible = false
            this.showMessage({
              type: 'success',
              msg: '保存成功'
            })
            this.$emit('save-finished')
          } else {
            this.showMessage(responseData)
          }
          this.resetLoad()
        })
        .catch((error) => {
          this.outputError(error)
        })
    },
    getFormById(id) {
      this.setLoad()
      return new Promise((resolve) => {
        getRoleById(id)
          .then((responseData) => {
            let form = {}
            if (responseData.code == 100) {
              form = responseData.data
            } else {
              this.showMessage(responseData)
            }
            this.resetLoad()
            resolve(form)
          })
          .catch((error) => {
            this.outputError(error)
          })
      })
    },
    onDialogClose() {
      this.visible = false
    },
    onDialogOpen() {
      this.$nextTick(() => {
        this.$refs.editForm.clearValidate()
      })
    },
    initForm() {
      return {
        company: { id: '' },
        code: '',
        name: '',
        isLocked: '0',
        remarks: ''
      }
    },
    async openView(id) {
      this.mode = 'view'
      this.title = '查看角色'
      this.form = {
        ...this.initForm(),
        ...(await this.getFormById(id))
      }
      this.initOptions()
      this.visible = true
    },
    async openEdit(id) {
      this.mode = 'edit'
      this.title = '修改角色'
      this.form = {
        ...this.initForm(),
        ...(await this.getFormById(id))
      }
      this.initOptions()
      this.visible = true
    },
    openAdd(parent) {
      this.mode = 'add'
      this.title = '添加角色'
      this.form = this.initForm()
      if(parent) {
        this.form.company.id = parent.company.id
      }
      this.initOptions()
      this.visible = true
    },
    async openCopy(id) {
      this.mode = 'add'
      this.title = '添加角色'
      this.form = {
        ...this.initForm(),
        ...(await this.getFormById(id))
      }
      this.initOptions()
      this.form.id = null
      this.visible = true
    },
    initCompanyOptions() {
      let search_List = { params: [] }
      treeCompany(search_List).then((responseData) => {
        if (responseData.code == 100) {
          this.companyOptions = responseData.data
        } else {
          this.showMessage(responseData)
        }
      })
    },
    initOptions() {
      this.initCompanyOptions()
    }
  }
}
</script>